<template>
  <div class="cart-container">
    <el-card>
      <div slot="header">
        <span>购物车</span>
      </div>
      <el-table :data="cartItems">
        <el-table-column prop="name" label="菜品名称"></el-table-column>
        <el-table-column prop="price" label="单价">
          <template slot-scope="scope">
            ¥{{ scope.row.price }}
          </template>
        </el-table-column>
        <el-table-column label="数量">
          <template slot-scope="scope">
            <el-input-number 
              v-model="scope.row.quantity" 
              :min="1" 
              @change="updateQuantity(scope.row.id, $event)">
            </el-input-number>
          </template>
        </el-table-column>
        <el-table-column label="小计">
          <template slot-scope="scope">
            ¥{{ scope.row.price * scope.row.quantity }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="danger" size="small" @click="removeFromCart(scope.row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="cart-footer">
        <span class="total">总计: ¥{{ cartTotal }}</span>
        <el-button type="primary" @click="checkout">结算</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  computed: {
    cartItems() {
      return this.$store.state.cartItems
    },
    cartTotal() {
      return this.$store.getters.cartTotal
    }
  },
  methods: {
    updateQuantity(dishId, quantity) {
      this.$store.commit('updateQuantity', { dishId, quantity })
    },
    removeFromCart(dishId) {
      this.$store.commit('removeFromCart', dishId)
      this.$message.success('已从购物车移除')
    },
    checkout() {
      if (this.cartItems.length === 0) {
        this.$message.warning('购物车是空的')
        return
      }
      
      this.$confirm('确认提交订单吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 这里可以添加提交订单的逻辑
        this.$store.commit('clearCart')
        this.$message({
          type: 'success',
          message: '订单提交成功！',
          duration: 2000
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消提交'
        })
      })
    }
  }
}
</script>

<style scoped>
.cart-container {
  padding: 20px;
}
.cart-footer {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
.total {
  margin-right: 20px;
  font-size: 18px;
  font-weight: bold;
  color: #f56c6c;
}
</style> 